package com.apware.utils.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.apware.utils.config.Config;
import com.apware.utils.entity.CarPartsInfo;


public class CarPartsInfoDao {
	static Logger log = Logger.getLogger(CarPartsInfoDao.class);
	final String TABLENAME = "tblSendStock";
	/**
	 * 获取未同步的商品
	 * @return
	 */
	public List<CarPartsInfo> getCarPartsList() {

		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<CarPartsInfo> carpartsList = new ArrayList<CarPartsInfo>();
		try {
			conn = DbConnectionManager.getConnection(Config.dbName);
			
			if (conn == null)
				return null;

			String sql = "SELECT cname,partsno,standardpartsno,isoem,manufacturerCname"
					+ ",manufacturerEname,ename,batchprice,unit,carinfo,producingArea,price,CostPrice,stock,OwnNo FROM "
								+ TABLENAME +" WHERE OwnNo='A01' and SendPost='N' ORDER BY partsNo desc";

			pst = conn.prepareStatement(sql);
			rs = pst.executeQuery();

			while (rs.next()) {

				CarPartsInfo c = new CarPartsInfo();
				c.setCname(rs.getString(1));
				c.setPartsNo(rs.getString(2));
				c.setStandardPartsNo(rs.getString(3));
				c.setIsOem(rs.getString(4));
				c.setManufactureCname(rs.getString(5));
				c.setManufactureEname(rs.getString(6));
				c.setEname(rs.getString(7));
				c.setBatchPrice(rs.getDouble(8));
				c.setUnit(rs.getString(9));
				c.setCarInfo(rs.getString(10));
				c.setProducingArea(rs.getString(11));
				c.setPrice(rs.getDouble(12));
				c.setCostPrice(rs.getDouble(13));
				c.setStock(rs.getLong(14));
				c.setOwnNo(rs.getString(15));

				carpartsList.add(c);
			} 
		} catch (Exception e) {
			log.error("获取未同步的商品失败！", e);
		} finally {
			try {
				try {
					if (rs != null) {
						rs.close();
						rs = null;
					}
				} finally {

					try {
						if (pst != null) {
							pst.close();
							pst = null;
						}
					} finally {
						if (conn != null) {
							conn.close();
							conn = null;
						}
					}
				}

			} catch (Exception e2) {
				// TODO: handle exception
			}
		}

		return carpartsList;
	}

	
	/**
	 * 修改同步状态
	 * @param PartsNo 商品编号
	 * * @param OwnNo 门店
	 * @return
	 */
	public boolean updateCarpartStatus(String PartsNo,String OwnNo) {
		Connection conn = null;
		PreparedStatement pst = null;
		boolean result = false;

		try {
			conn = DbConnectionManager.getConnection(Config.dbName);

			if (conn == null)
				return false;

			String sql = "UPDATE tblSendStock SET SendMark='Y', SendDate=GETDATE() Where partsNo = '" + PartsNo + "' AND OwnNo='" + OwnNo + "'";

			pst = conn.prepareStatement(sql);

			int affectCount = pst.executeUpdate();
			
			if (affectCount>0)
				result = true;
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		} finally {
			try {

				try {
					if (pst != null) {
						pst.close();
						pst = null;
					}
				} finally {
					if (conn != null) {
						conn.close();
						conn = null;
					}
				}

			} catch (Exception e2) {
				// TODO: handle exception
			}
		}

		return result;
	}
	
}
